// Dom7
var $$ = Dom7;

// Framework7 App main instance
var app = new Framework7({
    root: '#app', // App root element
    id: 'io.framework7.testapp', // App bundle ID
    name: 'Framework7', // App name
    theme: 'ios', // Automatic theme detection
    view: {
        stackPages: true, // 多级页面缓存
        pushState: true, // 开启 url 根据路由 hash 变化
    },
    // App root data
    data: function() {
        return {};
    },
    // App root methods
    methods: {
        getData(url) {
            // this.request.get('http://192.168.0.138/index9.html#/', { foo: 'bar', id: 5 }, function(data) {
            this.request.get(
                url, {},
                function(data) {
                    console.log('data :', data);
                    $$('.preloader').css({ 'display': 'none' })
                    window.location.href = href;
                },
                function(xhr, status) {
                    $$('.preloader').css({ 'display': 'none' })
                    console.log('xhr:', xhr);
                    console.log('status:', status);
                    if (status === 404) {
                        toastFun('请求的页面或链接不存在')
                    } else {
                        toastFun('请求页面失败')
                    }
                },
            );
        },
    },
});



// Init/Create views
var homeView = app.views.create('.view-main', {
    url: '/',
});


function toastFun(message) {
    var toastError = app.toast.create({
        icon: app.theme === 'ios' ? '<i class="f7-icons">close</i>' : '<i class="close-icons">提示</i>',
        text: message,
        position: 'center',
        closeTimeout: 2000,
    });
    toastError.open()
}

function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

var href = getQueryString('href');

app.methods.getData(href)


$$('.back').on('click', function() {
    history.go(-1); // 也是返回当前页的上一页， 不过表单里的数据全部还在
});

$$('#reload').on('click', function() {
    app.methods.getData(href)
        // window.location.href = href;
});
//